package top.healthylife.controller;/*
 *@Author GaoZeXi
 *@Created time 2020/4/25 4:51
 *@Description:
 * Step by Step  and Stand on,  You Are The Best Investment!!!
 */

import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.healthylife.constant.MessageConstant;
import top.healthylife.domain.Menu;
import top.healthylife.domain.Role;
import top.healthylife.domain.User;
import top.healthylife.entity.Result;
import top.healthylife.service.RoleService;
import top.healthylife.service.UserService;

import java.util.*;

/**
 * @author GaoZeXi
 * 根据用户权限展示菜单
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private UserService userService;

    @Reference
    private RoleService roleService;

    @RequestMapping("/getMenuByUser")
    public Result getMenuByUser(String username){
        try {
            User loginUser = userService.findByUsername(username);
            //用户对应的角色
            Set<Role> roles = roleService.findRolesByUser(loginUser);
            Set<Object> menuSet = new LinkedHashSet<>();
            for (Role role : roles) {
                menuSet.addAll(role.getMenus());
            }
            return new Result(true, MessageConstant.GET_MENU_SUCCESS,menuSet);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }


    }
}
